package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.bean.BeanCopier;
import com.ruoyi.system.domain.JzArticleShop;
import com.ruoyi.system.domain.JzToursShop;
import com.ruoyi.system.domain.vo.JzToursArticleReturnVo;
import com.ruoyi.system.domain.vo.JzToursArticleVo;
import com.ruoyi.system.domain.vo.ScenicRouteVo;
import com.ruoyi.system.mapper.JzArticleShopMapper;
import com.ruoyi.system.mapper.JzToursShopMapper;
import com.ruoyi.system.service.IFeginOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.JzToursArticleMapper;
import com.ruoyi.system.domain.JzToursArticle;
import com.ruoyi.system.service.IJzToursArticleService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文章资讯Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-03-18
 */
@Service
public class JzToursArticleServiceImpl implements IJzToursArticleService 
{
    @Autowired
    private JzToursArticleMapper jzToursArticleMapper;
    @Autowired
    private JzArticleShopMapper jzArticleShopMapper;
    @Autowired
    private JzToursShopMapper jzToursShopMapper;

    @Autowired
    private IFeginOutService feginOutService;




    /**
     * 查询文章资讯
     * 
     * @param id 文章资讯ID
     * @return 文章资讯
     */
    @Override
    public JzToursArticleReturnVo selectJzToursArticleById(Long id)
    {

        JzToursArticleReturnVo jzToursArticleReturnVo=new JzToursArticleReturnVo();
            JzToursArticle jzToursArticle=    jzToursArticleMapper.selectJzToursArticleById(id);
            JzArticleShop jzArticleShop=new JzArticleShop();
            jzArticleShop.setArticleId(jzToursArticle.getId());
         List<JzArticleShop>  jzArticleShops= jzArticleShopMapper.selectJzArticleShopList(jzArticleShop);
        BeanCopier.copy(jzToursArticle,jzToursArticleReturnVo, BeanCopier.CopyStrategy.IGNORE_NULL, BeanCopier.NoMatchingRule.IGNORE, null);
        if(jzArticleShops!=null && jzArticleShops.size()>0){
            //定义变量
            Integer type=-1;
            List<JzToursShop> shops=new ArrayList<>();
            for(JzArticleShop jzArticleShop1: jzArticleShops){

//                if(jzArticleShop1.getCateId()!=null && jzArticleShop1.getCateId()==1005){
//                    JzToursShop jzToursShop=new JzToursShop();
//                    JzToursArticle jzToursArticle1=  jzToursArticleMapper.selectJzToursArticleById(jzArticleShop1.getCateId());
//                    jzToursShop.setCateId(jzToursArticle1.getCateId());
//                    jzToursShop.setId(jzToursArticle1.getId());
//                    jzToursShop.setName(jzToursArticle1.getTitle());
//                    shops.add(jzToursShop);
//                }else {
                    JzToursShop jzToursShop=  jzToursShopMapper.selectJzToursShopById(jzArticleShop1.getShopId());
                    shops.add(jzToursShop);
                //}


            }

//            if(type==1) {
//                List<Long> collect = jzArticleShops.stream().map(JzArticleShop::getShopId).collect(Collectors.toList());
//               shops = jzToursShopMapper.selectJzToursShopListByIds(collect);
//            }else {
//
//
//
//            }
         jzToursArticleReturnVo.setShops(shops);

        }
        return jzToursArticleReturnVo;
    }

    /**
     * 查询文章资讯列表
     * 
     * @param jzToursArticle 文章资讯
     * @return 文章资讯
     */
    @Override
    public List<JzToursArticle> selectJzToursArticleList(JzToursArticle jzToursArticle)
    {
        return jzToursArticleMapper.selectJzToursArticleList(jzToursArticle);
    }

    /**
     * 新增文章资讯
     * 
     * @param jzToursArticle 文章资讯
     * @return 结果
     */
    @Override
    @Transactional
    public int insertJzToursArticle(JzToursArticleVo jzToursArticle)
    {

        JzToursArticle jzToursArticle1=new JzToursArticle();
        BeanCopier.copy(jzToursArticle,jzToursArticle1, BeanCopier.CopyStrategy.IGNORE_NULL, BeanCopier.NoMatchingRule.IGNORE, null);
        jzToursArticle1.setOutId(new Random().nextLong());
    int  total= jzToursArticleMapper.insertJzToursArticle(jzToursArticle1);

    if(jzToursArticle.getShopIds()!=null) {
        List<String> Jso = Arrays.asList(jzToursArticle.getShopIds().split(","));
        for(String s:Jso){
            JzArticleShop jzArticleShop=new JzArticleShop();
            jzArticleShop.setArticleId(jzToursArticle1.getId());
            jzArticleShop.setShopId(Long.parseLong(s));
            jzArticleShop.setCateId(jzToursArticle1.getCateId());
            jzArticleShopMapper.insertJzArticleShop(jzArticleShop);
        }
    }



    //如果新增的文章为路线 则需要同步
    if(jzToursArticle.getCateId()!=null && jzToursArticle.getCateId()==1002){

        ScenicRouteVo scenicRouteVo=new ScenicRouteVo();
        scenicRouteVo.setDescription(jzToursArticle.getContent());
        scenicRouteVo.setId(jzToursArticle1.getOutId());
        scenicRouteVo.setDuration("2小时");
        scenicRouteVo.setName(jzToursArticle.getTitle());
        scenicRouteVo.setPriority(jzToursArticle.getSort());
        scenicRouteVo.setVoice(jzToursArticle.getVoice());
        scenicRouteVo.setVoiceName(jzToursArticle.getVoiceName());
        JSONArray array=new JSONArray();

        if(jzToursArticle.getShopIds()!=null) {
            JSONObject jsonObject = new JSONObject();
            List<String> Jso = Arrays.asList(jzToursArticle.getShopIds().split(","));
            for (String s : Jso) {
                jsonObject = new JSONObject();
                JzToursShop jzToursShop=  jzToursShopMapper.selectJzToursShopById(Long.parseLong(s));
                jsonObject.put("mark",2);
                jsonObject.put("longtitude",jzToursShop.getxCoordinate());
                jsonObject.put("latitude",jzToursShop.getyCoordinate());
                array.add(jsonObject);
            }

        }
        scenicRouteVo.setRoute(array.toJSONString());

        feginOutService.syncScenicRoute(scenicRouteVo);




    }

        return 1;

    }

    /**
     * 修改文章资讯
     * 
     * @param jzToursArticle 文章资讯
     * @return 结果
     */
    @Override
    @Transactional
    public int updateJzToursArticle(JzToursArticleVo jzToursArticle)

    {
        JzToursArticle jzToursArticle1=new JzToursArticle();
        BeanCopier.copy(jzToursArticle,jzToursArticle1, BeanCopier.CopyStrategy.IGNORE_NULL, BeanCopier.NoMatchingRule.IGNORE, null);
     int total=   jzToursArticleMapper.updateJzToursArticle(jzToursArticle1);
        //先删 后增
        jzArticleShopMapper.deleteByArticle(jzToursArticle.getId());
        if(jzToursArticle.getShopIds()!=null && jzToursArticle.getShopIds()!="") {
            List<String> Jso = Arrays.asList(jzToursArticle.getShopIds().split(","));

            for(String s:Jso){
                JzArticleShop jzArticleShop=new JzArticleShop();
                jzArticleShop.setArticleId(jzToursArticle1.getId());
                jzArticleShop.setShopId(Long.parseLong(s));
                jzArticleShop.setCateId(jzToursArticle1.getCateId());
                jzArticleShopMapper.insertJzArticleShop(jzArticleShop);
            }
        }



        JzToursArticle jzToursArticle2=    jzToursArticleMapper.selectJzToursArticleById(jzToursArticle.getId());

        if(jzToursArticle.getCateId()!=null && jzToursArticle.getCateId()==1002 && jzToursArticle2.getOutId()!=null){

            ScenicRouteVo scenicRouteVo=new ScenicRouteVo();
            scenicRouteVo.setDescription(jzToursArticle.getContent());
            scenicRouteVo.setId(jzToursArticle2.getOutId());
            scenicRouteVo.setDuration("2小时");
            scenicRouteVo.setName(jzToursArticle.getTitle());
            scenicRouteVo.setPriority(jzToursArticle.getSort());
            scenicRouteVo.setVoice(jzToursArticle.getVoice());
            scenicRouteVo.setVoiceName(jzToursArticle.getVoiceName());
            JSONArray array=new JSONArray();

            if(jzToursArticle.getShopIds()!=null) {
                JSONObject jsonObject = new JSONObject();
                List<String> Jso = Arrays.asList(jzToursArticle.getShopIds().split(","));
                for (String s : Jso) {
                    jsonObject = new JSONObject();
                    JzToursShop jzToursShop=  jzToursShopMapper.selectJzToursShopById(Long.parseLong(s));
                    jsonObject.put("mark",2);
                    jsonObject.put("longtitude",jzToursShop.getxCoordinate());
                    jsonObject.put("latitude",jzToursShop.getyCoordinate());
                    array.add(jsonObject);
                }

            }
            scenicRouteVo.setRoute(array.toJSONString());

            feginOutService.syncScenicRoute(scenicRouteVo);




        }






        return 1;
    }

    /**
     * 批量删除文章资讯
     * 
     * @param ids 需要删除的文章资讯ID
     * @return 结果
     */
    @Override
    public int deleteJzToursArticleByIds(Long[] ids)
    {
        return jzToursArticleMapper.deleteJzToursArticleByIds(ids);
    }

    /**
     * 删除文章资讯信息
     * 
     * @param id 文章资讯ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteJzToursArticleById(Long id)
    {

        JzToursArticle jzToursArticle2=    jzToursArticleMapper.selectJzToursArticleById(id);

         feginOutService.deleteScenicRoute(jzToursArticle2.getOutId());

        return jzToursArticleMapper.deleteJzToursArticleById(id);
    }





}
